package com.kfy.rxjavademo;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * @ProjectName: RxJavaDemo
 * @Package: com.kfy.rxjavademo
 * @Author: K.F.Y
 * @Date: 2021/5/13
 */
public class RxJava {

    private static final String TAG = "RxJava";


    public static void testDemo1(){
        //Demo 1 : 依次输出 Hello、Hi、Good、Android、JAVA
        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("completed");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("error");
            }

            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
        };

        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("Hello");
                subscriber.onNext("Hi");
                subscriber.onNext("Good");
                subscriber.onNext("Android");
                subscriber.onNext("JAVA");
                //onError和onCompleted只能调用一个，彼此互斥
                // subscriber.onError(null);
                subscriber.onCompleted();
            }
        });

        observable.subscribe(subscriber);
    }




    public static void testDemo2(){

        //Demo 2 ：用 just 改进 Demo 1
        Observable<String> observable = Observable.just("Hello", "Hi", "Good","Android","JAVA");
        // 将会依次调用：
        //subscriber.onNext("Hello");
        //subscriber.onNext("Hi");
        //subscriber.onNext("Good");
        //subscriber.onNext("Android");
        //subscriber.onNext("JAVA");
        //subscriber.onCompleted();

        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("completed");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("error");
            }

            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
        };

        observable.subscribe(subscriber);
    }



    public static void testDemo3(){
        //Demo 3 ：用 from 改进 Demo 1
        String[] words = {"Hello", "Hi", "Good","Android","JAVA"};
        Observable.from(words).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("completed");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("error");
            }

            @Override
            public void onNext(String s) {
                System.out.println(s);
            }
        });
    }



    public static void testDemo4(){
        //将字符串数组 names 中的所有字符串依次打印出来
        String[] names = {"小红","小白","小黑","小明"};
        Observable.from(names)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });
    }

    public static void testDemo5(MainActivity activity){
        //由 id 取得图片并显示
        int drawableRes = R.mipmap.ic_launcher;
        Observable.create(new Observable.OnSubscribe<Drawable>() {

            @Override
            public void call(Subscriber<? super Drawable> subscriber) {
                Drawable drawable = activity.getDrawable(drawableRes);
                subscriber.onNext(drawable);
                subscriber.onCompleted();
            }

        }).subscribe(new Observer<Drawable>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(activity, "Error!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(Drawable drawable) {
                activity.showImage(drawable);
            }
        });
    }

    public static void testDemo6(){
        // 由于 subscribeOn(Schedulers.io()) 的指定，被创建的事件的内容 1、2、3、4 将会在 IO 线程发出；
        // 而由于 observeOn(AndroidScheculers.mainThread()) 的指定，因此 subscriber 数字的打印将发生在主线程 。
        Observable.just(1,2,3,4)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer number) {
                        Log.d("KFYTEST","number = "+ number );
                    }
                });

    }

    public static void testDemo7(MainActivity activity){
        //Demo 5 加上线程指定
        int drawableRes = R.mipmap.ic_launcher_round;
        Observable.create(new Observable.OnSubscribe<Drawable>() {

            @Override
            public void call(Subscriber<? super Drawable> subscriber) {
                Drawable drawable = activity.getDrawable(drawableRes);
                subscriber.onNext(drawable);
                subscriber.onCompleted();
            }

        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Drawable>() {

                    @Override
                    public void onCompleted() {
                        Toast.makeText(activity, "Completed!", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(activity, "Error!", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(Drawable drawable) {
                        activity.showImage(drawable);
                    }
                });

    }

    public static void testDemo8(){
        //变换  map()
        String[] strs = {"第1个","第2个","第3个","第4个","第5个","第6个","第7个"};
        Observable.just(1,3,5) // 输入类型 int
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) { // 参数类型 int
                        return strs[integer]; // 返回类型 String
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String  str) { // 参数类型 String
                        System.out.println("String:" + str);
                    }
                });
    }

    public static void testDemo9(){
        //变换 map()
        String[] strs = {"第1个","第2个","第3个","第4个","第5个","第6个","第7个"};
        Integer[] nums = {1,5,3};
        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onCompleted() {
                System.out.println("completed");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("error");
            }

            @Override
            public void onNext(String s) {
                System.out.println("String:" + s);
            }
        };
        Observable.from(nums)
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return strs[integer];
                    }
                })
                .subscribe(subscriber);
    }

    public static void testDemo10(){
        //复杂变换 flatMap()

        class Student{
            String name;
            Integer[] grades;
            public Student(String name,Integer[] grades){
                this.name = name;
                this.grades = grades;
            }
        }

        Student[] students = {
                new Student("小红",new Integer[]{90,90,100}),
                new Student("小白",new Integer[]{91,94,80}),
                new Student("小栗",new Integer[]{57,80,70}),
                new Student("小天",new Integer[]{95,90,90}),
                new Student("小澜",new Integer[]{100,100,100})
        };

        Subscriber<Integer> subscriber = new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                System.out.println("completed");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("error");
            }

            @Override
            public void onNext(Integer grade) {
                System.out.println("Grade:" + grade);
            }
        };

        Observable.from(students)
                .flatMap(new Func1<Student, Observable<Integer>>() {
                    @Override
                    public Observable<Integer> call(Student student) {
                        System.out.println("Student:" + student.name);
                        return Observable.from(student.grades);
                    }
                })
                .subscribe(subscriber);

    }

}
